﻿function Map2D( baseUri) {
	this._baseUri = baseUri;
	this._terrains = null;
	this._inViews = [];
	this._width = 0;
	this._height = 0;
	this._focusX = 0;
	this._focusY = 0;
	this._view = rect2d.create(0, 0, 0, 0);
}

function parseTerrainDesc( text) {
	var r = new Object();
	var lines = text.split( "\n");
	for ( var k in lines) {
		var fields = lines[k].split( " ");
		if ( fields.length < 2)
			continue;
		if ( fields[0] == "width")
			r.width = parseInt( fields[1]);
		else if ( fields[0] == "height")
			r.height = parseInt( fields[1]);
		else if ( fields[0] == "row")
			r.row = parseInt( fields[1]);
		else if ( fields[0] == "col")
			r.col = parseInt( fields[1]);
	}
	return r;
}

function parseMap2DDesc( text) {
    var r = {};
    r.terrains = [];
    var lines = text.split( "\n");
    for ( var k in lines) {
        var fields = lines[k].split( " ");
        if ( fields.length < 2)
            continue;
        if ( fields[0] == "width")
            r.width = parseInt( fields[1]);
        else if ( fields[0] == "height")
            r.height = parseInt( fields[1]);
        if ( fields.length < 5)
            continue;
        var t = {};
        t.uri = fields[0];
        t.x = parseInt( fields[1]);
        t.y = parseInt( fields[2]);
        t.w = parseInt( fields[3]);
        t.h = parseInt( fields[4]);
        t.terrains.push( t);
    }
    return r;
}

Map2D.prototype = {
_onLoadTrnDescOk : function( desc) {
	this._width = desc.width;
	this._height = desc.height;
	this._terrains = [];
	var pt = new PicTerrain( this._baseUri, desc.width, desc.height, desc.row, desc.col);
	pt.setVisibleArea( this._view);
	this._terrains.push( pt);
	this._inViews.push( pt);
},

_onLoadMap2DDescOk : function( desc) {
	this._width = desc.width;
	this._height = desc.height;
	this._terrains = [];
    var v = rect2d.create();
    for ( var k in desc.terrains) {
        var t = desc.terrains[k];
        var pt = new PicTerrain( r.uri, r.w, t.h);
        pt.x = t.x;
        pt.y = t.y;
        if ( rect2d.interRect( this._view, v)) {
            pt.setVisibleArea( v);
            this._inViews.push( pt);
        }
        this._terrains.push( pt);
    }
},

load : function( callback) {
	var trnDesc = this._baseUri + "desc.txt";
	var _This = this;
	$.get( trnDesc, function( text, status) {
		if ( status == "success") {
			var r = parseTerrainDesc( text);
			_This._onLoadTrnDescOk( r);
			if ( callback)
				callback();
		}
	});
},

loadMulti : function( callback) {
    var mapDesc = this._baseUri + "desc.txt";
    var _This = this;
    $.get( mapDesc, function( text, status) {
        if ( status == "success") {
           var t = parseMap2DDesc( text);
           _This._onLoadMap2DDescOk( r);
           if ( callback)
                callback();  
        }
    });
},



setView : function( x, y, w, h) {
	if ( x < 0) x = 0;
	if ( y < 0) y = 0;
	if ( w > this._width) w = this._width;
	if ( h > this._height) h = this._height;
	if ( x + w > this._width) x = this._width - w;
	if ( y + h > this._height) y = this._height - h;
	this._focusX = x + w / 2;
	this._focusY = y + h / 2;
	this._view.x = x;
	this._view.y = y;
	this._view.w= w;
	this._view.h = h;
	if ( this._terrains.length == 1) {
		this._terrain[0].setVisibleArea( this._view);
	} else {
	    this._inViews = [];
	    var v = rect2d.create();
	    for ( var k in this._terrains) {
	        var pt = this._terrains[k];
	        if ( rect2d.interRect( this._view, pt)) {
	            pt.setVisibleArea( v);
	            this._inViews.push( pt);
	        }
	    }
	}
},

setFocus : function( x, y) {
	var vx = x - this._view.w / 2;
	if ( vx >= 0 && vx + this._view.w <= this._width) {
		this._focusX = x;
		this._view.x = vx;
	}
	else {
		if ( vx < 0) vx = 0;
		if ( vx + this._view.w > this._width) vx = this._width - this._view.w;
		this._view.x = vx;
		this._focusX = vx + this._view.w / 2;
	}

	var vy = y - this._view.h / 2;	
	if ( vy >= 0 && vy + this._view.h <= this._height) {
		this._focusY = y;
		this._view.y = vy;
	}
	else {
		if ( vy < 0) vy = 0;
		if ( vy + this._view.h > this._height) vy = this._height - this._view.h;
		this._view.x = vx;
		this._focusY = vy + this._view.h / 2;
	}
	
	if ( this._terrain != null)
		this._terrain.setVisibleArea( this._view.x, this._view.y, this._view.w, this._view.h);
},

screenToMap : function( x, y) {
	return new Point2D( this._view.x + x, this._view.y + y);
},

mapToScreen : function( x, y) {
	return new Point2D( x - this._view.x, y - this._view.y);
},

update : function( tickTime) {
},

draw : function( context) {
	if ( this._terrain != null)
		this._terrain.draw( context);
},

}   // end Map.prototype